/**
 * @file luogu/4071/main.cpp
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @brief 错位排列问题，f[i] = (i-1)*f[i-1] + (i-1)*f[i-2]
 * @version 0.1
 * @date 2022-04-29
 * @note SDOI 卡常数数正常现象
 *
 * @copyright Copyright (c) 2022
 *
 */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
const int N = 1000010;
ll factorial[N];
ll f[N];
ll qexp(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) res *= a, res %= mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
inline ll inv(ll x) { return qexp(x, mod - 2); }

void init() {
  factorial[0] = 1;
  for (ll i = 1; i < N; ++i) factorial[i] = factorial[i - 1] * i % mod;
  f[0] = 1, f[1] = 0, f[2] = 1;
  for (ll i = 3; i < N; ++i)
    f[i] = ((i - 1) * f[i - 1] % mod + (i - 1) * f[i - 2] % mod) % mod;
}
inline ll C(ll x, ll y) {
  return factorial[x] * inv(factorial[x - y]) % mod * inv(factorial[y]) % mod;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init();
  ll tc;
  cin >> tc;
  while (tc--) {
    ll n, m;
    cin >> n >> m;
    ll ans = C(n, m) * f[n - m] % mod;
    cout << ans << endl;
  }
}
